<template>
  <div>
    <el-form :inline="true" :model="queryForm" class="demo-form-inline">
      <el-form-item label="菜单名称">
        <el-input v-model="queryForm.name" placeholder="菜单名称" clearable />
      </el-form-item>
      <el-form-item>
        <el-button v-permission="'system:menu:query'" type="primary" @click="handleQuery">查询</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-permission="'system:menu:insert'" type="primary" plain icon="Plus" @click="handleAdd('0')">新增</el-button>
      </el-col>
    </el-row>

    <el-table ref="treeRef" :data="tree" row-key="id" v-loading="loading" :default-expanded-keys="[tree[0] ? tree[0].id : '0']" :tree-props="{children:'children'}" :filter-node-method="filterNode">
      <el-table-column label="菜单名称" align="left" prop="name" />
      <el-table-column label="菜单类型" align="left" prop="type" width="100">
		<template #default="{row}">
		  <el-tag v-if="row.type == 1">目录</el-tag>
		  <el-tag type="success" v-if="row.type == 2">菜单</el-tag>
		  <el-tag type="danger" v-if="row.type == 3">tab页</el-tag>
		  <el-tag type="warning" v-if="row.type == 4">按钮</el-tag>
		  <el-tag type="info" v-if="row.type == 5">超链接</el-tag>
		  <el-tag type="info" v-if="row.type == 6">后台接口</el-tag>
		  <el-tag type="info" v-if="row.type == 7">其他</el-tag>
		</template>
	  </el-table-column>			
      <el-table-column label="路由" align="left" prop="route" />
      <el-table-column label="图标" align="left" prop="icon" width="80">
		  <template #default="scope">
			<el-icon :size="20"><component :is="scope.row.icon"/></el-icon>
		  </template>
	  </el-table-column>
      <el-table-column label="权限标识" align="left" prop="perms" :show-overflow-tooltip="true" width="200"/>
	  <el-table-column label="路径" align="left" prop="url" />
	  <el-table-column label="排序" align="left" prop="sort" width="80"/>
      <el-table-column width="200">
        <template #default="scope">
		  <el-button v-permission="'system:menu:insert'" link type="primary" icon="el-icon-plus" @click="handleAdd(scope.row.id)">新增</el-button>
          <el-button v-permission="'system:menu:update'" link type="primary" icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button v-permission="'system:menu:delete'" link type="primary" icon="Delete" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

	<EditForm v-on:callBack="getTreeTable" ref="editForm"></EditForm>
  </div>
</template>
<script setup lang="ts">
import { ref, reactive, nextTick, toRef } from "vue";
import baseService from "@/service/baseService";
import { ElMessage, ElMessageBox } from "element-plus";
import EditForm from './editform.vue';

// 查询参数
const queryForm = reactive({
  name: ""
});
// 列表内容数量
const total = ref(0);
// 列表是否加载
const loading = ref(true);

let treeRef = ref([]);
let tree = ref([]);

const editForm = ref(); //ref方法是让我们的简单类型数据变成响应式数据;在方法里改变时需要.value来获取

// 表单实例
const formRef = ref();
// 是否打开弹出框
const open = ref(false);
// 弹出框标题
const title = ref("");
// 提交表单数据
let form = toRef(
  reactive({
    id: "",
    name: "",
    route: "",
    type: "",
    parentId: "",
	linkType: "",
	perms: "",
	icon: "",
	url: "",
	sort: ""
  })
);


/**
 * 查询列表
 */
const getTreeTable = () => {
  loading.value = true;
  baseService
    .get("/system/menu/treeTable", queryForm)
    .then((res) => {
      loading.value = false;
      if (res.code === 200) {
        tree.value = res.data;
      } else {
        tree.value = [];
      }
    })
    .catch(() => {
      loading.value = false;
    });
};

const filterNode = (value, data) => {
  if (!value) return true
  return data.name.indexOf(value) !== -1
}

/**
 * 搜索按钮操作
 */
function handleQuery() {
  queryForm.pageNo = 1;
  getTreeTable();
}

/**
 * 删除按钮操作
 * @param id 主键
 */
function handleDelete(id: any) {
  ElMessageBox.confirm("确认要删除当前项吗?", "提示").then(() => {
    baseService.delete(`/system/menu/delete`, id).then((res) => {
      if (res.code === 200) {
        ElMessage.success(res.msg);
        getTreeTable();
      } else {
        ElMessage.error(res.msg);
      }
    });
  });
}

//新增按钮操作
const handleAdd = (parentId : string) => {
   editForm.value.addFormShow('新增菜单', parentId)
};

//定义类型
interface Menu {
  id: string
  name: string
  route: string
  type: number
  parentId: string
  linkType: number
  perms: string
  icon: string
  url: string
  sort: number
};

//编辑
const handleUpdate = (row: Menu) => {
  editForm.value.updateFormShow('修改菜单', row)
};

getTreeTable();
</script>

<style scoped></style>
